package com.egao.orderB.biz.impl;

import com.egao.common.core.damon.constant.UserConstant;
import com.egao.common.core.damon.results.BaseDao;
import com.egao.common.core.damon.results.PageResult2;
import com.egao.common.core.damon.utils.Utils;
import com.egao.common.core.web.JsonResult;
import com.egao.common.system.entity.User;
import com.egao.orderB.biz.OceanProcessBiz;
import com.egao.orderB.entity.OceanProcess;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * 数据层实现类
 * @author : Doman
 * @date : 2020-12-07 17:03:04
 * @version ：1.0
 */
@Repository
public class OceanProcessBizImpl extends BaseDao implements OceanProcessBiz {

	@Override
	public JsonResult list(PageResult2<T> page, OceanProcess oceanProcess, User loginUser) throws Exception {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<OceanProcess> rm = new BeanPropertyRowMapper<OceanProcess>(OceanProcess.class);
		StringBuffer sql = new StringBuffer("select o.id,o.organization_id,o.ocean_id");
        sql.append(",o.state,o.state_time,o.remark,o.create_time");
		sql.append(",o.create_user,o.update_time,o.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.username createName ");
        sql.append(" from chf_ocean_process o ");
        sql.append(" inner join sys_user suser on suser.user_id=o.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=o.organization_id ");
        sql.append(" where o.deleted=0 and sogzn.deleted=0 ");
		if(loginUser.getUserId() != UserConstant.ADMIN_ID){
            sql.append(" and o.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
    	if(Utils.isNotEmpty(oceanProcess)){
//			if(Utils.isNotEmpty(oceanProcess.getName())){
//				sql.append(" and instr(o.name,:name)>0 ");
//				map.put("name", oceanProcess.getName());
//			}

			if(Utils.isNotEmpty(oceanProcess.getOceanId())){
				sql.append(" and o.ocean_id=:ocean_id ");
				map.put("ocean_id", oceanProcess.getOceanId());
			}
			if(Utils.isNotEmpty(oceanProcess.getSort())&&Utils.isNotEmpty(oceanProcess.getOrder())){
                sql.append(" order by o."+oceanProcess.getSort()+" "+oceanProcess.getOrder());
            } else {
                sql.append(" order by o.create_time desc");
            }
		}
		return jsonResult(getPageQueryList(page, sql.toString(), map, rm));
	}

	@Override
	public List<OceanProcess> list(OceanProcess oceanProcess, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<OceanProcess> rm = new BeanPropertyRowMapper<OceanProcess>(OceanProcess.class);
        StringBuffer sql = new StringBuffer("select o.id,o.organization_id,o.ocean_id");
        sql.append(",o.state,o.state_time,o.remark,o.create_time");
		sql.append(",o.create_user,o.update_time,o.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.username createName ");
        sql.append(" from chf_ocean_process o ");
        sql.append(" inner join sys_user suser on suser.user_id=o.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=o.organization_id ");
        sql.append(" where o.deleted=0 and sogzn.deleted=0 ");
		if(loginUser.getUserId() != UserConstant.ADMIN_ID){
            sql.append(" and o.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
    	if(Utils.isNotEmpty(oceanProcess)){
//			if(Utils.isNotEmpty(oceanProcess.getName())){
//				sql.append(" and instr(o.name,:name)>0 ");
//				map.put("name", oceanProcess.getName());
//			}
			if(Utils.isNotEmpty(oceanProcess.getOceanId())){
				sql.append(" and o.ocean_id=:ocean_id ");
				map.put("ocean_id", oceanProcess.getOceanId());
			}
            if(Utils.isNotEmpty(oceanProcess.getSort())&&Utils.isNotEmpty(oceanProcess.getOrder())){
				sql.append(" order by o."+oceanProcess.getSort()+" "+oceanProcess.getOrder());
			} else {
				sql.append(" order by o.create_time desc");
			}
		}
		getMapParameter(sql, map);
		return jdbcTemplate.query(sql.toString(),map, rm);
	}

	@Override
	public List<OceanProcess> list(OceanProcess oceanProcess) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<OceanProcess> rm = new BeanPropertyRowMapper<OceanProcess>(OceanProcess.class);
		StringBuffer sql = new StringBuffer("select b.id,b.organization_id,b.ocean_id");
		sql.append(",b.state,b.state_time,b.remark,b.create_time");
		sql.append(",b.create_user,b.update_time,b.update_user");
		sql.append(",sognz.organization_name organizationName,suser.username createName ");
		sql.append(" from chf_ocean_process b ");
		sql.append(" inner join sys_user suser on suser.user_id=b.create_user ");
		sql.append(" inner join sys_organization sognz on sognz.organization_id=b.organization_id ");
		sql.append(" where b.deleted=0 and sognz.deleted=0  and b.ocean_id=:ocean_id ");
		map.put("ocean_id", oceanProcess.getOceanId());
		if(Utils.isNotEmpty(oceanProcess)){
//			if(Utils.isNotEmpty(oceanProcess.getName())){
//				sql.append(" and instr(b.name,:name)>0 ");
//				map.put("name", oceanProcess.getName());
//			}
			if(Utils.isNotEmpty(oceanProcess.getSort())&&Utils.isNotEmpty(oceanProcess.getOrder())){
				sql.append(" order by "+oceanProcess.getSort()+" "+oceanProcess.getOrder());
			} else {
				sql.append(" order by b.create_time desc");
			}
		}
		getMapParameter(sql, map);
		return jdbcTemplate.query(sql.toString(),map, rm);
	}

	@Override
	public OceanProcess getById(OceanProcess oceanProcess) {
		RowMapper<OceanProcess> rm = new BeanPropertyRowMapper<OceanProcess>(OceanProcess.class);
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql = new StringBuffer("select o.id,o.organization_id,o.ocean_id");
        sql.append(",o.state,o.state_time,o.remark,o.create_time");
		sql.append(",o.create_user,o.update_time,o.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.username createName ");
        sql.append(" from chf_ocean_process o ");
        sql.append(" inner join sys_user suser on suser.user_id=o.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=o.organization_id ");
        sql.append(" where o.deleted=0 and sogzn.deleted=0 and o.id=:id ");
		map.put("id", oceanProcess.getId());
		getMapParameter(sql, map);
		return jdbcTemplate.queryForObject(sql.toString(), map, rm);
	}
	@Override
	public boolean saveWidthCreateTime(OceanProcess oceanProcess, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("insert into chf_ocean_process (");
		sql.append("organization_id,ocean_id,state,state_time");
		sql.append(",remark,create_user,create_time) values ( ");
		sql.append(":organization_id,:ocean_id,:state,now()");
		sql.append(",:remark,:create_user,:create_time)");
		map.put("organization_id", loginUser.getOrganizationId() + "");
		map.put("ocean_id", oceanProcess.getOceanId());
		map.put("state", oceanProcess.getState());
		map.put("remark", oceanProcess.getRemark());
		map.put("create_user", loginUser.getUserId() + "");
		map.put("create_time", oceanProcess.getCreateTime());
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}
	@Override
	public boolean save(OceanProcess oceanProcess, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("insert into chf_ocean_process (");
        sql.append("organization_id,ocean_id,state,state_time");
		sql.append(",remark,create_user,create_time) values ( ");
        sql.append(":organization_id,:ocean_id,:state,:state_time");
		sql.append(",:remark,:create_user,now())");
        map.put("organization_id", loginUser.getOrganizationId() + "");
        map.put("ocean_id", oceanProcess.getOceanId());
        map.put("state", oceanProcess.getState());
        map.put("state_time", oceanProcess.getStateTime());
        map.put("remark", oceanProcess.getRemark());
		map.put("create_user", loginUser.getUserId() + "");
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean update(OceanProcess oceanProcess, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update chf_ocean_process set ");
        sql.append("state=:state,state_time=:state_time ");
		sql.append(",remark=:remark,update_time=now(),update_user=:update_user where id=:id ");
        map.put("ocean_id", oceanProcess.getOceanId());
        map.put("state", oceanProcess.getState());
        map.put("state_time", oceanProcess.getStateTime());
        map.put("remark", oceanProcess.getRemark());
		map.put("update_user", loginUser.getUserId() + "");
		map.put("id", oceanProcess.getId());
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean remove(OceanProcess oceanProcess, User loginUser) {
		int count = 0;
		Map<String, String> map = null;
		if(oceanProcess.getId().length()>0){
			for(String ID : oceanProcess.getId().split(",")){
				map  = new HashMap<String, String>();
				if(ID==null || ID.equals("")){ break; }
				StringBuffer sql =new StringBuffer("update chf_ocean_process set deleted=1" +
						",update_user=:update_user, update_time=now() "
						+ " where id =:id ");
				map.put("update_user", loginUser.getUserId() + "");
				map.put("id", ID);
				getMapParameter(sql, map);
				count = jdbcTemplate.update(sql.toString(),map);
			}
		}
		return count > 0;
	}
}
